package com.example.yoga.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yoga.entity.CourseMv;
import com.example.yoga.entity.User;
import com.example.yoga.service.ICourseMvService;
import com.example.yoga.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenkai
 * @since 2021-02-05
 */
@RestController
@RequestMapping("/courseMv")
public class CourseMvController {


    @Autowired
    private ICourseMvService courseMvService;

    /**
     * 修改用户（普通会员或者教练）
     */
    @RequestMapping(value = "/delCourseMv",method = RequestMethod.DELETE)
    public JsonResult<Void> delCourseMv(@RequestBody CourseMv courseMv) {
        System.out.println(courseMv.toString()+"************************");
        Integer mvid = courseMv.getMvid();
        Integer courseid = courseMv.getCourseid();

        QueryWrapper<CourseMv> courseMvQueryWrapper = new QueryWrapper<>();
        courseMvQueryWrapper.eq("mvId",mvid);
        courseMvQueryWrapper.eq("courseId",courseid);
        boolean b = courseMvService.remove(courseMvQueryWrapper);
        if (b){
            return new JsonResult<>(2000,"移除成功");
        }
        return new JsonResult<>(4000,"移除失败");
    }


    /**
     * 添加课程与视频记录 add
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JsonResult<Void> add(@RequestBody CourseMv courseMv) {
        System.out.println(courseMv.toString()+"************************");
        if(courseMv==null){
            return new JsonResult<>(4000,"添加失败");
        }
        //判断是否已有此纪录
        QueryWrapper<CourseMv> courseMvQueryWrapper = new QueryWrapper<>();
        courseMvQueryWrapper.eq("mvId",courseMv.getMvid());
        courseMvQueryWrapper.eq("courseId",courseMv.getCourseid());
        List<CourseMv> list = courseMvService.list(courseMvQueryWrapper);
        if (list.size()>0){
            return new JsonResult<>(4000,"添加失败,该视频已存在");
        }
        boolean b = courseMvService.save(courseMv);
        if (b){
            return new JsonResult<>(2000,"添加成功");
        }
        return new JsonResult<>(4000,"添加失败");
    }

}

